<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="菜单名称" prop="type">
        <el-select
          v-model="queryParams.type"
          style="width: 215px"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in menuList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createById">
        <el-select
          v-model="queryParams.createById"
          placeholder="请选择创建人"
          clearable
          filterable
          size="small"
          style="width: 215px"
          remote
          @focus="searchUser()"
          :remote-method="searchUser"
        >
          <el-option
            v-for="item in createByOptions"
            :key="item.userId"
            :label="item.realName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运营标签" prop="labelName">
        <el-input
          v-model="queryParams.labelName"
          placeholder="请输入运营标签"
          clearable
          size="small"
          style="width: 215px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人" prop="updateById">
        <el-select
          v-model="queryParams.updateById"
          placeholder="请选择更新人"
          clearable
          filterable
          size="small"
          style="width: 215px"
          remote
          @focus="searchUser()"
          :remote-method="searchUser"
        >
          <el-option
            v-for="item in createByOptions"
            :key="item.userId"
            :label="item.realName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="valuedate">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.valuedate"
          style="width: 215px"
          @change="handleOrderDateChange"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="valueUpdateTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.valueUpdateTime"
          style="width: 215px"
          @change="handleUpdateDateChange"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['system:label:add']"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="labelList"
      v-adaptive="{ bottomOffset: 50 }"
      height="100px"
    >
      <el-table-column
        label="菜单名称"
        min-width="150"
        align="center"
        prop="type"
      >
        <template slot-scope="scope">
          <span>{{ statue(scope.row.type, menuList) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="运营标签"
        min-width="150"
        align="center"
        prop="labelName"
      >
        <template slot-scope="scope">
          <span
            :style="{
              backgroundColor: scope.row.labelColor,
              color: '#fff',
              padding: '10px 10px',
            }"
            >{{ scope.row.labelName }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="提示信息"
        min-width="150"
        align="center"
        prop="cueInfo"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        min-width="150"
        align="center"
        prop="createBy"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        min-width="150"
        align="center"
        prop="createTime"
      />
      <el-table-column
        label="更新时间"
        min-width="150"
        align="center"
        prop="updateTime"
      />

      <el-table-column
        label="操作"
        width="100"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:label:edit']"
            style="margin-right: 10px"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDeletel(scope.row)"
            v-hasPermi="['system:label:remove']"
            style="margin-right: 10px"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改商品分类对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="640px"
      append-to-body
      @close="closeDialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="16">
            <el-form-item
              label="菜单名称"
              prop="type"
              :rules="{
                required: true,
                message: '请选择菜单名称',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="form.type"
                :disabled="title == '运营标签修改'"
                style="width: 250px"
                placeholder="请选择状态"
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in menuList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item
              label="运营标签"
              prop="labelName"
              :rules="{
                required: true,
                message: '请输入运营标签',
                trigger: 'blur',
              }"
            >
              <el-input
                style="width: 250px"
                v-model="form.labelName"
                placeholder="请输入运营标签"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item
              label="提示信息"
              prop="cueInfo"
            >
              <el-input
                style="width: 250px"
                v-model="form.cueInfo"
                placeholder="请输入提示信息"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item
              label="标签颜色"
              prop="labelColor"
              :rules="{
                required: true,
                message: '请选择标签颜色',
                trigger: 'change',
              }"
            >
              <div class="tagColor">
                <div
                  v-for="(item, index) in tagColorList"
                  :key="index"
                  :style="{
                    backgroundColor: item.dictValue,
                    color: item.dictValue,
                  }"
                  @click="getColor(item, index)"
                >
                  <i class="el-icon-check" :style="{ color: item.remark }"></i>
                  <el-radio
                    v-show="false"
                    v-model="form.labelColor"
                    :label="item.dictValue"
                  ></el-radio>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div v-if="activate"></div>
  </div>
</template>

<script>
import {
  listUser,
  listLabel,
  getLabel,
  addLabel,
  updateLabel,
  getDeletel,
} from "@/api/common/label";
export default {
  name: "Label",
  components: {},
  data() {
    return {
      open: false,
      title: "",
      activate: true,
      // 遮罩层
      loading: false,
      form: {},
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 商品信息表格数据
      labelList: [],
      brandOptions: [],
      //创建人
      createByOptions: [],
      //菜单名称
      menuList: [],
      //标签颜色字典
      tagColorList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    //菜单名称字典 malls_label_type menuName
    this.getDicts("malls_label_type").then((res) => {
      this.menuList = res.data;
    });
    //标签颜色字典 cbi_sys_base_tagcolor
    this.getDicts("cbi_sys_base_tagcolor").then((res) => {
      this.tagColorList = res.data;
    });
    this.getList();
  },
  //触发回流
  activated() {
    this.activate = false;
    setTimeout(() => {
      this.activate = true;
    });
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listLabel(this.queryParams).then((response) => {
        this.labelList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询所有用户信息
    searchUser(name) {
      listUser({ realName: name }).then((response) => {
        this.createByOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },
    //创建时间范围查询
    handleOrderDateChange(v) {
      if (v) {
        this.queryParams.createStartTime = v[0] + " 00:00:00";
        this.queryParams.createEndTime = v[1] + " 23:59:59";
        console.log(this.queryParams.createEndTime);
      } else {
        this.queryParams.createStartTime = null;
        this.queryParams.createEndTime = null;
        this.getList();
      }
    },
    handleUpdateDateChange(v) {
      if (v) {
        this.queryParams.updateStartTime = v[0] + " 00:00:00";
        this.queryParams.updateEndTime = v[1] + " 23:59:59";
      } else {
        this.queryParams.updateStartTime = null;
        this.queryParams.updateEndTime = null;
        this.getList();
      }
    },
    getColor(v, index1) {
      this.tagColorList.forEach((item, index) => {
        if (index1 == index) {
          item.remark = "#fff";
          this.form.labelColor = item.dictValue;
          this.form = { ...this.form };
        } else {
          item.remark = "";
        }
      });
    },
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLabel(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLabel(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //弹框关闭事件
    closeDialog() {
      this.tagColorList.forEach((item, index) => {
        item.remark = null;
      });
      //清空表单校验
      this.$refs.form.resetFields();
      this.form = {};
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "运营标签新增";
      this.open = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      getLabel(row.id).then((res) => {
        this.form = res.data;
        console.log(this.form);
        this.tagColorList.forEach((item, index) => {
          if (this.form.labelColor == item.dictValue) {
            item.remark = "#fff";
            this.form.labelColor = item.dictValue;
            this.form = { ...this.form };
          } else {
            item.remark = "";
          }
        });
        this.title = "运营标签修改";
        this.open = true;
      });
    },
    handleDeletel(row) {
      this.$confirm(
        "是否确认删除名称为 “" + row.labelName + "” 的标签？",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        getDeletel(row.id).then((res) => {
          this.getList();
          this.msgSuccess("操作成功");
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.treeselect-main {
  width: 193px;
}
.tagColor {
  > div {
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 50%;
    margin-left: 10px;
  }

  //background-color: red;
  //display: inline-block;
  //margin: 5px;
  display: flex;
}
</style>
